﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BroRPG.GameComponents
{
    public class Sprite 
    {
        public Sprite(String spritesheet, String texture)
        {
            this._texture = texture;
            this._spritesheet = spritesheet;
            this._position = new Vector2(0, 0);
        }

        public Sprite(String spritesheet, String texture, Vector2 position)
        {
            this._texture = texture;
            this._spritesheet = spritesheet;
            this._position = position;
        }

        protected String _texture;
        public String Texture
        {
            get { return _texture; }
            set { _texture = value; }
        }

        protected String _spritesheet;
        public String Spritesheet
        {
            get { return _spritesheet; }
            set { _spritesheet = value; }
        }

        protected Vector2 _position;
        public Vector2 Position
        {
            get { return _position; }
            set { _position = value; }
        }

        public Vector2 Origin
        {
            get
            {
                return new Vector2(Constants.SpriteSheets[_spritesheet].SourceRectangle(_texture).Width/ 2.0f, Constants.SpriteSheets[_spritesheet].SourceRectangle(_texture).Height / 2.0f);
            }
        }

        protected Rectangle rectangle;
        public Rectangle Rectangle
        {
            get { return rectangle; }
        }

       
        public Matrix Transform
        {
            get;
            private set;
        }

        protected float rotation = 0f;
        public float Rotation
        {
            get { return rotation; }
            set { rotation = value; }
        }

        protected float opacity = 1.0f;
        public float Alpha
        {
            get { return opacity; }
            set { opacity = value; }
        }

        protected Color materialColor = Color.White;
        public Color MaterialColor
        {
            get
            {
                return materialColor;
            }
            set { materialColor = value; }
        }


        protected float _light = 0;
        public float Light
        {
            get
            {
                return _light;
            }
            set { 
                _light = value;
                if (_light < 0)
                    _light = 0;
               
            }
        }

        protected Color _lightColor = Color.Black;
        public Color LightColor
        {
            get { return _lightColor; }
            set { _lightColor = value; }
        }




        protected void CalculateMatrix()
        {
            Transform = Matrix.CreateTranslation(new Vector3(-Origin, 0)) *
                Matrix.CreateRotationZ(rotation) *
                Matrix.CreateScale(1.0f) *
                Matrix.CreateTranslation(new Vector3(_position, 0));
        }


        protected void CalculateBoundingRectangle()
        {
           
            rectangle = new Rectangle(0, 0, Constants.SpriteSheets[_spritesheet].SourceRectangle(_texture).Width, Constants.SpriteSheets[_spritesheet].SourceRectangle(_texture).Height);
            Vector2 leftTop = Vector2.Transform(new Vector2(rectangle.Left, rectangle.Top), Transform);
            Vector2 rightTop = Vector2.Transform(new Vector2(rectangle.Right, rectangle.Top), Transform);
            Vector2 leftBottom = Vector2.Transform(new Vector2(rectangle.Left, rectangle.Bottom), Transform);
            Vector2 rightBottom = Vector2.Transform(new Vector2(rectangle.Right, rectangle.Bottom), Transform);

            Vector2 min = Vector2.Min(Vector2.Min(leftTop, rightTop),
                                Vector2.Min(leftBottom, rightBottom));
            Vector2 max = Vector2.Max(Vector2.Max(leftTop, rightTop),
                                        Vector2.Max(leftBottom, rightBottom));

            rectangle = new Rectangle((int)min.X, (int)min.Y,
                (int)(max.X - min.X), (int)(max.Y - min.Y));
        
        }
              
        public virtual void Update(GameTime gameTime)
        {
            LightColor = Color.Black;
            Light = 0.0f;
            CalculateMatrix();
            CalculateBoundingRectangle();
        }


        public virtual void Draw(GameTime gameTime,float ambientPercent,  Color ambientColor)
        {

            Constants.SpritebatchBegin(ambientPercent, _light, MaterialColor, ambientColor, _lightColor, Alpha);
                Constants.Spritebatch.Draw(
                    Constants.SpriteSheets[_spritesheet].Texture,
                    new Rectangle((int)_position.X,
                                  (int)_position.Y,
                                  Constants.SpriteSheets[_spritesheet].SourceRectangle(_texture).Width,
                                  Constants.SpriteSheets[_spritesheet].SourceRectangle(_texture).Height),
                    Constants.SpriteSheets[_spritesheet].SourceRectangle(_texture),
                    Color.White,
                    rotation,
                    Origin,
                    SpriteEffects.None,
                    0.0f);
                Constants.SpritebatchEnd();
            
        }

        public void DrawBounds(GameTime gameTime, float ambientPercent, Color ambient)
        {
            if (bounds != null)
            {
                Constants.SpritebatchBegin(0, 1, MaterialColor, ambient, Color.Black, Alpha);
                Constants.Spritebatch.Draw(bounds, rectangle, Color.White);
                Constants.SpritebatchEnd();
            }
        }


        public static Texture2D bounds;
    }
}
